package model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Recenzja extends Model {

	private long id;

	public long getId() {
		return id;
	}

	public Recenzja(long _id) {
		id = _id;
	}
	
	public void delete() throws SQLException{
		PreparedStatement p = getConnection().prepareStatement(
				"delete from Recenzja where id = ?");
		p.setLong(1,id);
		p.executeUpdate();
		p.close();
	}
	
	public Recenzja(long film, String autor, String tresc) throws SQLException {
		id = getNextSequence("seq_Czlowiek");
		PreparedStatement p = getConnection().prepareStatement(
				"insert into Recenzja(id,film_id,uzytkownik_login,tresc) "
						+ "values(?,?, ?,?)");
		p.setLong(1, id);
		p.setLong(2, film);
		p.setString(3, autor);
		p.setClob(4, makeClob(tresc));
		p.executeUpdate();
		p.close();
	}

	public void ocen(String kto, int ocena) throws SQLException {
		PreparedStatement p = getConnection().prepareStatement(
				"insert into OcenilRecenzje(uzytkownik_login,recenzja_id,na) "
						+ "values(?, ?,?)");
		p.setString(1, kto);
		p.setLong(2, id);
		p.setInt(3, ocena);
		p.executeUpdate();
		p.close();
	}
	
	public RecenzjaBean get() throws SQLException  {
		PreparedStatement p = getConnection().prepareStatement( "select film_id,napisana,punkty_suma_cache,uzytkownik_login,punkty_ilosc_cache,tresc,id from RECENZJA where id  = ? ");
		p.setLong( 1, id );
		ResultSet r = p.executeQuery();
		if(r.next()){
			RecenzjaBean b = new RecenzjaBean(r.getLong(1), r.getDate(2), r.getLong(3), r.getString(4), r.getLong(5),readClob(r.getClob(6)), r.getLong(7));
			r.close();
			p.close();
			return b;
		} else {
			r.close();
			p.close();
			return null;
		}
	}
	
}
